#!/bin/bash
## 启动Docker daemon

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # 无颜色

# 简单的日志函数
log_info() { echo -e "${BLUE}[INFO] $1${NC}"; }
log_error() { echo -e "${RED}[ERROR] $1${NC}"; }
log_success() { echo -e "${GREEN}[SUCCESS] $1${NC}"; }

# 检查Docker daemon是否运行
check_docker() {
    if ! docker info > /dev/null 2>&1; then
        log_error "Docker daemon 未运行"
        return 1
    else
        log_success "Docker daemon 已运行"
        return 0
    fi
}

# 启动Docker daemon (针对macOS)
start_docker() {
    log_info "尝试启动Docker daemon..."
    open -a Docker
    # 等待Docker启动
    for i in {1..30}; do
        if docker info > /dev/null 2>&1; then
            log_success "Docker daemon 启动成功"
            return 0
        fi
        log_info "等待Docker启动... ($i/30)"
        sleep 2
    done
    log_error "Docker daemon 启动超时"
    return 1
}

# 检查并启动Docker
check_docker
if [ $? -ne 0 ]; then
    start_docker
    if [ $? -ne 0 ]; then
        log_error "无法启动Docker，退出脚本"
        exit 1
    fi
fi
